package com.frikinet.lifeguard.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class Month extends ArrayList<Week>
{
	private static final long serialVersionUID = 1L;
	public static final SimpleDateFormat MONTH_NAME_FORMAT = new SimpleDateFormat("MMMM");
	
	private int year;
	private String month;
	private String name;

	public Month(int year, int month)
	{
		super();
		this.year = year;
		this.month = String.format("%02d", month);
		Calendar c = Calendar.getInstance();
		c.set(year, month - 1, 1);
		this.name = MONTH_NAME_FORMAT.format(c.getTime());
		int padding = ((c.get(Calendar.DAY_OF_WEEK) - c.getFirstDayOfWeek()) + 7) % 7;
		Week week = new Week(padding);
		while (c.get(Calendar.MONTH) == month - 1)
		{
			if (week.isFull())
			{
				add(week);
				week = new Week();
			}
			week.add(new Day(c));
			c.add(Calendar.DATE, 1);
		}
		week.fill();
		add(week);
	}

	public int getYear()
	{
		return year;
	}

	public String getMonth()
	{
		return month;
	}

	public String getName()
	{
		return name;
	}
}
